﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TTBusiness.DB_Linq;
using TTBusiness.ClassDB;
using TTBusiness;
namespace TTMarketing.Admin
{
    public partial class Image : System.Web.UI.Page
    {
        clsBSImage objDB = new clsBSImage();
        protected void Page_Load(object sender, EventArgs e)
        {
            divmessage.InnerHtml = "";
            divmessage2.InnerHtml = "";
        }

        protected void cmbClose_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (Convert.ToInt32(e.Value) == 1)
            {
                plDetail.Visible = true;
            }
            else
            {
                plDetail.Visible = false;
            }
        }

        protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(rgdGrid.Items[rgdGrid.SelectedIndexes[0]]["ID"].Text);
                tblImage mfc = new tblImage();
                mfc = objDB.getById(id);
                this.plDetail.Visible = true;
                this.txtSummary.Text = mfc.Summary;
                this.txtLink.Text = mfc.Link;
                this.cmbTypeImage.SelectedValue = mfc.TypeID.ToString();
                this.imgImage.ImageUrl = "/Image/Upload/" + mfc.Name;
                this.hfImageName.Value = mfc.Name;
                this.hfId.Value = id.ToString();
                this.cmbClose.SelectedValue = "1";
            }
            catch (Exception ex)
            {
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi : " + ex.Message).ToString();
            }
        }

        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            try
            {
                tblImage obj = new tblImage();
                obj.Name = this.rfUpload.UploadedFiles.Count > 0 ? this.rfUpload.UploadedFiles[0].FileName :
                    this.hfImageName.Value.ToString();
                obj.Summary = this.txtSummary.Text;
                obj.TypeID = Convert.ToInt32(this.cmbTypeImage.SelectedValue);
                obj.Link = this.txtLink.Text;
                objDB.insert(obj);
                divmessage.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Thêm mới thành công").ToString();
                this.rgdGrid.Rebind();
                this.imgImage.ImageUrl = "/Image/Upload/" + obj.Name;
                btnClear_Click(null, null);

            }
            catch (Exception ex)
            {
                divmessage.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể thêm mới : " + ex.Message).ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                tblImage obj = new tblImage();
                obj.Name = this.rfUpload.UploadedFiles.Count > 0 ? this.rfUpload.UploadedFiles[0].FileName :
                    this.hfImageName.Value.ToString();
                obj.ID = Convert.ToInt32(hfId.Value);
                obj.TypeID = Convert.ToInt32(this.cmbTypeImage.SelectedValue);
                obj.Link = this.txtLink.Text;
                obj.Summary = this.txtSummary.Text;
                objDB.update(obj);
                divmessage.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Cập nhật thành công").ToString();
                this.rgdGrid.Rebind();
                this.imgImage.ImageUrl = "/Image/Upload/" + obj.Name;

            }
            catch (Exception ex)
            {
                divmessage.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể cập nhật : " + ex.Message).ToString();
            }
        }

        protected void rgdGrid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("delete"))
                {
                    objDB.delete(Convert.ToInt32(e.CommandArgument));
                    divmessage2.InnerHtml = clsBSMessageAdmin.MessageSuccess("Thành công : ", "Đã xóa thành công!").ToString();
                    this.rgdGrid.Rebind();
                    if (this.plDetail.Visible && !String.IsNullOrEmpty(hfId.Value) && Convert.ToInt32(hfId.Value).Equals(Convert.ToInt32(e.CommandArgument)))
                    {
                        btnClear_Click(null, null);
                    }
                }
            }
            catch (Exception)
            {
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi không thể xóa được").ToString();
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtSummary.Text = "";
                this.txtLink.Text = "";
                this.hfId = null;
                this.hfImageName.Value = null;
                this.imgImage.ImageUrl = "";
            }
            catch (Exception ex)
            {

                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi " + ex.Message).ToString();
            }
        }

        protected void cmbProduct_DataBound(object sender, EventArgs e)
        {
            try
            {
                this.rgdGrid.Rebind();
                clsBSTypeImage objTI = new clsBSTypeImage();
                this.lblSizeImage.Text = objTI.getById(Convert.ToInt32(this.cmbTypeImage.SelectedValue)).Size;
            }
            catch (Exception ex)
            {

                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi " + ex.Message).ToString();
            }
        }

        protected void cmbTypeImage_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                this.rgdGrid.Rebind();
                clsBSTypeImage objTI = new clsBSTypeImage();
                this.lblSizeImage.Text = objTI.getById(Convert.ToInt32(this.cmbTypeImage.SelectedValue)).Size;
            }
            catch (Exception ex)
            {
                
                divmessage2.InnerHtml = clsBSMessageAdmin.MessageError("Lỗi : ", "Gặp lỗi " + ex.Message).ToString();
            }
        }
    }
}